package com.ourchem.bus.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ourchem.bus.domain.CollectManage;
import com.ourchem.bus.mapper.CollectManageMapper;
import com.ourchem.common.utils.DateUtils;
import com.ourchem.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ourchem.bus.mapper.OrganizationMapper;
import com.ourchem.bus.domain.Organization;
import com.ourchem.bus.service.IOrganizationService;

/**
 * 机构管理Service业务层处理
 * 
 * @author xiYan
 * @date 2023-01-12
 */
@Service
public class OrganizationServiceImpl implements IOrganizationService 
{
    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private CollectManageMapper collectManageMapper;

    /**
     * 查询机构管理
     * 
     * @param id 机构管理主键
     * @return 机构管理
     */
    @Override
    public Organization selectOrganizationById(Integer id)
    {

        Organization organization = organizationMapper.selectOrganizationById(id);

        CollectManage collectManage = new CollectManage();
        collectManage.setUserId(SecurityUtils.getUserId());
        collectManage.setCollectId(id.toString());
        List<CollectManage> list = collectManageMapper.selectCollectManageList(collectManage);
        if (list.size() > 0) {
            organization.setRemark("1");
        } else {
            organization.setRemark("0");
        }
        return organization;
    }

    /**
     * 查询机构管理列表
     * 
     * @param organization 机构管理
     * @return 机构管理
     */
    @Override
    public List<Organization> selectOrganizationList(Organization organization)
    {

        List<Organization> organizations = organizationMapper.selectOrganizationList(organization);
        CollectManage collectManage = new CollectManage();
        organizations.stream().map(v->{
            collectManage.setUserId(SecurityUtils.getUserId());
            collectManage.setCollectId(v.getId().toString());
            if (collectManageMapper.selectCollectManageList(collectManage).size() > 0) {
                v.setRemark("1");
            } else {
                v.setRemark("0");
            }
            return  v;
        }).collect(Collectors.toList());

        return  organizations;
    }

    /**
     * 新增机构管理
     * 
     * @param organization 机构管理
     * @return 结果
     */
    @Override
    public int insertOrganization(Organization organization)
    {
        organization.setCreateTime(DateUtils.getNowDate());
        organization.setCreateBy(SecurityUtils.getUsername());
        return organizationMapper.insertOrganization(organization);
    }

    /**
     * 修改机构管理
     * 
     * @param organization 机构管理
     * @return 结果
     */
    @Override
    public int updateOrganization(Organization organization)
    {
        organization.setUpdateTime(DateUtils.getNowDate());
        organization.setUpdateBy(SecurityUtils.getDeptName());
        return organizationMapper.updateOrganization(organization);
    }

    /**
     * 批量删除机构管理
     * 
     * @param ids 需要删除的机构管理主键
     * @return 结果
     */
    @Override
    public int deleteOrganizationByIds(Integer[] ids)
    {
        return organizationMapper.deleteOrganizationByIds(ids);
    }

    /**
     * 删除机构管理信息
     * 
     * @param id 机构管理主键
     * @return 结果
     */
    @Override
    public int deleteOrganizationById(Integer id)
    {
        return organizationMapper.deleteOrganizationById(id);
    }
}
